package com.algorithm.model;

import java.util.LinkedList;
import java.util.List;

public class TreeNodeService {

    public static TreeNode getTree() {
        TreeNode root = new TreeNode(3);
        TreeNode t2 = new TreeNode(9);
        TreeNode t3 = new TreeNode(20);
        TreeNode t4 = new TreeNode(15);
        TreeNode t5 = new TreeNode(7);
        TreeNode t6 = new TreeNode(11);
        TreeNode t7 = new TreeNode(12);

        root.left = t2;
        root.right = t3;
        t2.left = t6;
        t6.right = t7;
        t3.left = t4;
        t3.right = t5;

        return root;
    }


    public static TreeNode getTreeByArray(int[] arr) {
        List<TreeNode> treeNodes = new LinkedList<>();
        TreeNode treeNode = new TreeNode(arr[0]);
        treeNodes.add(treeNode);
        getTreeByArray(arr, 1, treeNodes);
        return treeNode;
    }

    public static void getTreeByArray(int[] arr, int i, List<TreeNode> treeNodes) {
        if (treeNodes == null || treeNodes.size() == 0) {
            return;
        }
        List<TreeNode> list = new LinkedList<>();
        for (TreeNode treeNode : treeNodes) {
            if (i >= arr.length) {
                return;
            }
            treeNode.left = new TreeNode(arr[i++]);
            if (i >= arr.length) {
                return;
            }
            treeNode.right = new TreeNode(arr[i++]);
            list.add(treeNode.left);
            list.add(treeNode.right);
        }
        getTreeByArray(arr, i, list);
    }

    public static TreeNode getTreeByArray(Integer[] arr) {

        LinkedList<TreeNode> list = new LinkedList<>();
        TreeNode root = new TreeNode(arr[0]);
        list.add(root);
        int i = 1;

        while (i < arr.length) {
            int s = list.size();

            for (int j = 0; j < s; j++) {
                TreeNode treeNode = list.removeFirst();
                if (treeNode == null) {
                    i = i + 2;
                } else {
                    treeNode.left = arr[i] == null ? null : new TreeNode(arr[i]);
                    i++;
                    treeNode.right = arr[i] == null ? null : new TreeNode(arr[i]);
                    i++;
                    list.add(treeNode.left);
                    list.add(treeNode.right);
                }

            }
        }

        return root;

    }

    public static void print(TreeNode treeNode) {

        LinkedList<TreeNode> list = new LinkedList<>();
        list.add(treeNode);
        boolean b = true;
        while (list.size() != 0 && b) {
            b = false;
            int s = list.size();
            for (int i = 0; i < s; i++) {
                treeNode = list.removeFirst();
                if (treeNode == null) {
                    System.out.print("null ");
                    list.add(null);
                    list.add(null);
                } else {
                    if (treeNode.left != null || treeNode.right != null) {
                        b = true;
                    }
                    System.out.println(treeNode.val + " ");
                    list.add(treeNode.left);
                    list.add(treeNode.right);

                }

            }
        }

    }


}
